home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_1.3 / Read-Me1.3 / Printer1.3 / Driver.Examples / src / okimate20 / render.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-01  |  4.8 KB  |  192 lines

  1. /*
  2.     Okimate_20 driver.
  3.     David Berezowski - Sept/87.
  4. */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/nodes.h>
  8. #include <exec/lists.h>
  9. #include <exec/memory.h>
  10. #include "../printer/printer.h"
  11. #include "../printer/prtbase.h"
  12.  
  13. #define NUMSTARTCMD    5    /* # of cmd bytes before binary data */
  14. #define NUMENDCMD    1    /* # of cmd bytes after binary data */
  15. #define NUMTOTALCMD     (NUMSTARTCMD + NUMENDCMD)    /* total of above */
  16. #define NUMLFCMD    3    /* # of cmd bytes for linefeed */
  17. #define MAXCOLORBUFS    3    /* max # of color buffers */
  18.  
  19. Render(ct, x, y, status)
  20. long ct, x, y, status;
  21. {
  22.     extern void *AllocMem(), FreeMem();
  23.     extern struct PrinterData *PD;
  24.     extern struct PrinterExtendedData *PED;
  25.  
  26.     static UWORD RowSize, ColorSize, BufSize, TotalBufSize, dataoffset;
  27.     static UWORD colors[MAXCOLORBUFS]; /* color ptrs */
  28.     static UBYTE CRLF[] = "\033Jn";
  29.     static UWORD NumColorBufs; /* actually # of color buffers */
  30.     static UWORD NumSpcCmd; /* # of cmd bytes at very beginning */
  31.     UBYTE *ptr, *ptrstart, *ptr2, *ptr2start;
  32.     int i, err;
  33.  
  34.     switch(status) {
  35.         case 0 : /* Master Initialization */
  36.             /*
  37.                 ct    - pointer to IODRPReq structure.
  38.                 x    - width of printed picture in pixels.
  39.                 y    - height of printed picture in pixels.
  40.             */
  41.             RowSize = x * 3;
  42.             ColorSize = RowSize + NUMTOTALCMD;
  43.             if (PD->pd_Preferences.PrintShade == SHADE_COLOR) {
  44.                 NumColorBufs = MAXCOLORBUFS;
  45.                 NumSpcCmd = 2;
  46.             }
  47.             else {
  48.                 NumColorBufs = 1;
  49.                 NumSpcCmd = 0;
  50.             }
  51.             BufSize = NumSpcCmd + ColorSize * NumColorBufs +
  52.                 NUMLFCMD;
  53.             TotalBufSize = BufSize * 2;
  54.             for (i=0; i<NumColorBufs; i++) {
  55.                 colors[i] = ColorSize * i; /* YMC or B */
  56.             }
  57.             PD->pd_PrintBuf = AllocMem(TotalBufSize, MEMF_PUBLIC);
  58.             if (PD->pd_PrintBuf == NULL) {
  59.                 err = PDERR_BUFFERMEMORY; /* no mem */
  60.             }
  61.             else {
  62.                 dataoffset = NumSpcCmd + NUMSTARTCMD;
  63.                 if (PD->pd_Preferences.PrintShade ==
  64.                     SHADE_COLOR) {
  65.                     /* align ribbon (1st buffer) */
  66.                     *PD->pd_PrintBuf = 27;
  67.                     *(PD->pd_PrintBuf + 1) = 25;
  68.                     /* align ribbon (2nd buffer) */
  69.                     *(PD->pd_PrintBuf + BufSize) = 27;
  70.                     *(PD->pd_PrintBuf + BufSize + 1) = 25;
  71.                 }
  72.                 err = PDERR_NOERR; /* all ok */
  73.             }
  74.             break;
  75.  
  76.         case 1: /* Scale, Dither and Render */
  77.             /*
  78.                 ct    - pointer to PrtInfo structure.
  79.                 x    - 0.
  80.                 y    - row # (range 0 to Height - 1).
  81.             */
  82.             Transfer(ct, y, &PD->pd_PrintBuf[dataoffset], colors);
  83.             err = PDERR_NOERR; /* all ok */
  84.             break;
  85.  
  86.         case 2 : /* Dump Buffer to Printer */
  87.             /*
  88.                 ct    - 0.
  89.                 x    - 0.
  90.                 y    - # of rows sent (1 to NumRows).
  91.  
  92.                 White-space strip.
  93.             */
  94.             ptrstart = &PD->pd_PrintBuf[dataoffset];
  95.             ptr2 = ptrstart - NUMSTARTCMD;
  96.             ptr2start = ptr2 - NumSpcCmd;
  97.             x = 0; /* flag no transfer required yet */
  98.             for (ct=0; ct<NumColorBufs;
  99.                 ct++, ptrstart += ColorSize) {
  100.                 i = RowSize;
  101.                 ptr = ptrstart + i - 1;
  102.                 while (i > 0 && *ptr == 0) {
  103.                     i--;
  104.                     ptr--;
  105.                 }
  106.                 i = (i + 2) / 3;
  107.                 ptr = ptrstart - NUMSTARTCMD;
  108.                 *ptr++ = 27;
  109.                 *ptr++ = '%';
  110.                 *ptr++ ='O';        /* enter 24-dot mode */
  111.                 *ptr++ = i & 0xff;
  112.                 *ptr++ = i >> 8;    /* size */
  113.                 i *= 3;
  114.                 *(ptrstart + i) = 13; /* cr */
  115.                 i += NUMTOTALCMD;
  116.                 if (x != 0) { /* if must transfer data */
  117.                     /* get src start */
  118.                     ptr = ptrstart - NUMSTARTCMD;
  119.                     do { /* transfer and update dest ptr */
  120.                         *ptr2++ = *ptr++;
  121.                     } while (--i);
  122.                 }
  123.                 else { /* no transfer required */
  124.                     ptr2 += i; /* update dest ptr */
  125.                 }
  126.                 /* if compacted or 0 */
  127.                 if (i != RowSize + NUMTOTALCMD) {
  128.                     x = 1; /* we need to xfer next time */
  129.                 }
  130.             }
  131.             y += (y + 1) / 2; /* convert 144ths to 216ths */
  132.             *ptr2++ = 0x1b;
  133.             *ptr2++ = 'J';
  134.             *ptr2++ = y;    /* y/216 lf */
  135.             i = ptr2 - ptr2start; /* calc size */
  136.             /* if completely empty */
  137.             if (i == NumSpcCmd + NUMTOTALCMD * NumColorBufs +
  138.                 NUMLFCMD) {
  139.                 CRLF[2] = y;
  140.                 /* y/216 lf */
  141.                 err = (*(PD->pd_PWrite))(CRLF, NUMLFCMD);
  142.             }
  143.             else {
  144.                 err = (*(PD->pd_PWrite))(ptr2start, i);
  145.             }
  146.             if (err == PDERR_NOERR) {
  147.                 dataoffset = (dataoffset == NumSpcCmd +
  148.                     NUMSTARTCMD ? BufSize : 0) +
  149.                     NumSpcCmd + NUMSTARTCMD;
  150.             }
  151.             break;
  152.  
  153.         case 3 : /* Clear and Init Buffer */
  154.             /*
  155.                 ct    - 0.
  156.                 x    - 0.
  157.                 y    - 0.
  158.             */
  159.             ptr = &PD->pd_PrintBuf[dataoffset];
  160.             i = BufSize - NumSpcCmd - NUMTOTALCMD - NUMLFCMD;
  161.             do {
  162.                 *ptr++ = 0;
  163.             } while (--i);
  164.             err = PDERR_NOERR; /* all ok */
  165.             break;
  166.  
  167.         case 4 : /* Close Down */
  168.             /*
  169.                 ct    - error code.
  170.                 x    - io_Special flag from IODRPReq struct.
  171.                 y    - 0.
  172.             */
  173.               /* wait for both buffers to empty */
  174.             (*(PD->pd_PBothReady))();
  175.             if (PD->pd_PrintBuf != NULL) {
  176.                 FreeMem(PD->pd_PrintBuf, TotalBufSize);
  177.             }
  178.             err = PDERR_NOERR; /* all ok */
  179.             break;
  180.  
  181.         case 5 : /* Pre-Master Initialization */
  182.             /*
  183.                 ct    - 0 or pointer to IODRPReq structure.
  184.                 x    - io_Special flag from IODRPReq struct.
  185.                 y    - 0.
  186.             */
  187.             err = PDERR_NOERR; /* all ok */
  188.             break;
  189.     }
  190.     return(err);
  191. }
  192.